package se.teamscape.shared.model;

import java.util.ArrayList;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * 
 * Model/entity representation of a team.
 * 
 * @author Andreas Borglin
 * 
 */
@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Team implements Model {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String encodedKey;

    @Persistent
    private String name;

    @Persistent
    private String ownerPersonKey;

    @NotPersistent
    private ArrayList<TeamPlayer> players;

    public Team() {

    }

    @Override
    public String getId() {
        return encodedKey;
    }

    @Override
    public String getDisplayName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setOwnerPersonKey(String ownerPersonKey) {
        this.ownerPersonKey = ownerPersonKey;
    }

    public String getOwnerPersonKey() {
        return ownerPersonKey;
    }

    public void setPlayers(ArrayList<TeamPlayer> players) {
        this.players = players;
    }

    public void addPlayer(TeamPlayer player) {
        this.players.add(player);
    }

    public ArrayList<TeamPlayer> getPlayers() {
        return players;
    }

    public TeamPlayer getPlayerAt(int index) {
        if (index < 0 || index >= this.players.size()) {
            return null;
        }

        return this.players.get(index);
    }

}
